const mysql=require('mysql2');
const dbConfig=require('./dbconfig');
const pool=mysql.createPool(dbConfig);

//封装query只sql，args可传可不传
function query({sql,args}){
    return new Promise((resolve,reject)=>{
        try{
            pool.getConnection(function(err,connection){
                if(err){
                    reject(err)
                }else{
                    console.log('sql',sql)
                    console.log('args',args)
                    if(args){
                        connection.query(sql,args,function(cErr,rows){
                            pool.releaseConnection(connection);
                            if(cErr){
                                reject(cErr)
                            }else{
                                resolve(rows)
                            }
                        })
                    }else{
                        connection.query(sql,function(cErr,rows){
                            pool.releaseConnection(connection);
                            if(cErr){
                                reject(cErr)
                            }else{
                                resolve(rows)
                            }
                        })
                    }
                }
            })
        }catch(e){
            console.log(e)
        }
    })


}

module.exports={
    query:query
}
